#pragma once

#include "Headers.h"
#include <time.h>
#pragma comment (lib,"Winmm.lib")


#define WAIT_TIME .01
class Clock
{
private:
	double ElapsedTime;
	double TotalGameTime;
	double Now,Then;
	int FPSCount;
	int FramesSoFar;
	bool NeedToUpdate;
public:
	Clock():ElapsedTime(0), TotalGameTime(0), NeedToUpdate(false)
	,Now(0),Then(0),FPSCount(0),FramesSoFar(0){}
	~Clock(){}
	void Init();
	double Run();
	bool TICK();
	void GameUpdated(){NeedToUpdate = false;}
	double GetTotalPlayTime(){return TotalGameTime;}
	double GetTimeInterval(){return Now - Then * .001f;}
};

void Clock::Init()
{
	ElapsedTime = 0;
	TotalGameTime = 0;
	NeedToUpdate = false;
	Now = timeGetTime();
	Then = timeGetTime();
	FPSCount = 0;
	FramesSoFar = 0;
	Run();
}

double Clock::Run()
{
	FramesSoFar++;
	//Updates now
	Now = timeGetTime();

	double TimeTemp = ( Now - Then ) * .001f;
	ElapsedTime += TimeTemp;
	TotalGameTime += TimeTemp;

	Then = Now;
	//If it has been a second, update and reset
	if(ElapsedTime >= WAIT_TIME)
	{
		//TICK(SETTIME);
		ElapsedTime = 0;
		FramesSoFar = 0;
	}
	//Update fps count
	if(FramesSoFar > 1 && ElapsedTime > 0)
		FPSCount = (int)(FramesSoFar / ElapsedTime);
	//std::stringstream out;out << "\nTime Elapsed: " << TotalGameTime << "\nFrames Per Second: " << FPSCount;
	//std::string s = out.str();
	//Graphics::Access()->AddToDebugText(s);
	return TimeTemp;
}